package cn.edu.lcu.cs.javaprogramming.basic;

import java.util.Scanner;

/**
 * <p>Switch语句演示</p>
 * <ol>
 * <li>传统的Switch</li>
 * <li>Java12引入的增强型Switch</li>
 * </ol>
 * 2022/3/1 18:54
 *
 * @Author ling
 */
public class SwitchDemo {
    public static void main(String[] args) {
        System.out.println("请输入0-100之间的成绩");
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();
        System.out.println("成绩等级为：" + grade(score));
    }

    /**
     * 使用Java14增强型Switch，对成绩进行分级。
     *
     * @param score
     * @return
     */
    public static String grade(int score) {
        return switch (score / 10) {
            case 10, 9 -> "优";
            case 8 -> "良";
            case 7 -> "中";
            case 6 -> "差";
            default -> "不及格";
        };
    }

    /**
     * 使用传统型Switch，对成绩进行分级。
     *
     * @param score
     * @return
     */
    public static String gradeClassic(int score) {
        switch (score / 10) {
            case 10:
            case 9:
                return "优";
            case 8:
                return "良";
            case 7:
                return "中";
            case 6:
                return "差";
            default:
                return "不及格";
        }
    }
}
